package com.fita.admin.handler.impl;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;
import com.fita.admin.handler.SmsHandler;
import com.fita.admin.rao.SmsRao;
import com.fita.admin.web.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
/**
 * 
 * @author sjh
 *
 */
@Component("smsHandler")
public class SmsHandlerImpl implements SmsHandler{
	
	private  static final Logger logger = LoggerFactory.getLogger(SmsHandlerImpl.class);
	
	@Value("#{config['sms.host']}")
	private String host;
	
	@Value("#{config['sms.mock']}")
	private boolean mock;
	 
	@Value("#{ctx['ctx.domain']}")
	private String systemHost;
	
	@Rao
	private SmsRao smsRao;

	/*
	 * (non-Javadoc)
	 * @see com.fita.admin.handler.SmsHandler#sendOrderCancelMessage(java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean sendOrderCancelMessage(String mobile, String code, String url) {
		
		HttpResult httpResult = new HttpResult();
		
		String message = "【"+ Constant.SMS_ORDER_COMPANY_NAME+"】尊敬的会员，您预定的" + code + "已取消，重新预定请戳" + systemHost + url;
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		
		parameters.put("message", message);
		
		parameters.put("mobiles", mobile);
		
		parameters.put("time", "0");
		
		String sendUrl = host + "/send-sms";
		
		logger.info("url is [{}]", sendUrl);
		
		logger.info("pramas is [{}]", JsonUtils.toJson(parameters));
		
		logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobiles"));
		
		httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSender(sendUrl, parameters);
		
		logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));
		
		return httpResult.isValid();
	}

    /* (non-Javadoc)
     * @see com.fita.admin.handler.SmsHandler#sendMarkPayMessage(java.lang.String, java.lang.String)
     */
    public boolean sendMarkPayMessage(String mobile, String content) {

        HttpResult httpResult = new HttpResult();
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("message", content);
        parameters.put("mobiles", mobile);
        parameters.put("time", "0");
        logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobile"));
        
        String url = host +"/send-sms";
        logger.info("url is [{}]", url);
        logger.info("pramas is [{}]", JsonUtils.toJson(parameters));
        
        httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSender(url, parameters);
        
        logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));
        
        return httpResult.isValid();
    }


	public boolean sendAuditSuccessMessage(String mobile, String content) {

		HttpResult httpResult = new HttpResult();
		HashMap<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("message", content);
		parameters.put("mobiles", mobile);
		parameters.put("time", "0");
		logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobiles"));

		String url = host +"/send-sms";
		logger.info("url is [{}]", url);
		logger.info("pramas is [{}]", JsonUtils.toJson(parameters));

		httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSender(url, parameters);

		logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));

		return httpResult.isValid();
	}

	/**
	 * 发送消息
	 *  @param mobile
	 * @param message
	 * @return
	 */
	public boolean sendMessage(String mobile, String message) {

		HttpResult httpResult = new HttpResult();
		HashMap<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("message", message);
		parameters.put("mobiles", mobile);
		parameters.put("time", "0");
		logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobiles"));

		String url = host +"/send-sms";
		logger.info("url is [{}]", url);
		logger.info("pramas is [{}]", JsonUtils.toJson(parameters));

		httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSender(url, parameters);

		logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));

		return httpResult.isValid();

	}

	/**
	 *
	 * @param mobile
	 * @param messageTemplate
	 * @param values
	 * @return
	 */
	public boolean sendMessage(String mobile, String messageTemplate, Map values){

		String message = StringUtils.replacePlaceHolder(messageTemplate, values);

		return this.sendMessage(mobile, message);
	}
}
